package org.lex.notepad.ui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.lex.swing.widgets.EnhancedTextArea;


public class NotepadEditor extends EnhancedTextArea {
	private static final int BUFFER_SIZE = 8 * 1024;
	private Charset charset = null;

	public NotepadEditor() {
		super();
		this.setTextAntialiasEnabled(true);
	}

	public Charset getCharset() {
		return this.charset;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}

	public void read(InputStream in) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset), BUFFER_SIZE);
		this.clear();
		read(reader, charset);
		setDocumentChanged(false);
	}

	public void write(OutputStream out) throws IOException {
		final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset), BUFFER_SIZE);
		setDocumentChanged(false);
		write(writer);
	}
}
